using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;

namespace Contracts
{
    public interface INavigationItem
    {
        string Name { get; }
        string Icon { get; }
        string NavigationPath { get; }
    }

    public interface IModuleEventAggregator
    {
        void PublishModulesLoaded();

        void SubscribeModulesLoaded(Action action);
    }

    public class ModuleEventAggregator : IModuleEventAggregator
    {
        private readonly IEventAggregator _eventAggregator;
        private readonly ModulesLoadedEvent _modulesLoadedEvent;

        public ModuleEventAggregator(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            _modulesLoadedEvent = _eventAggregator.GetEvent<ModulesLoadedEvent>();
        }

        public void PublishModulesLoaded()
        {
            _modulesLoadedEvent.Publish();
        }

        public void SubscribeModulesLoaded(Action action)
        {
            _modulesLoadedEvent.Subscribe(action);
        }
    }

    public class ModulesLoadedEvent : PubSubEvent
    { }

    public interface INavigationRegistry : INotifyCollectionChanged, INotifyPropertyChanged
    {
        void RegisterNavigationItem(INavigationItem item);

        IEnumerable<INavigationItem> NavigationItems { get; }
    }

    public class NavigationRegistry : INavigationRegistry
    {
        private readonly ObservableCollection<INavigationItem> _navigationItems = new ObservableCollection<INavigationItem>();

        public event NotifyCollectionChangedEventHandler CollectionChanged
        {
            add => _navigationItems.CollectionChanged += value;
            remove => _navigationItems.CollectionChanged -= value;
        }

        public event PropertyChangedEventHandler PropertyChanged
        {
            add => ((INotifyPropertyChanged)_navigationItems).PropertyChanged += value;
            remove => ((INotifyPropertyChanged)_navigationItems).PropertyChanged -= value;
        }

        public void RegisterNavigationItem(INavigationItem item)
        {
            _navigationItems.Add(item);
        }

        public IEnumerable<INavigationItem> NavigationItems => _navigationItems;
    }
}